package com.busi.controller.binding;

import java.util.HashMap;
import java.util.Map;

import com.busi.model.BindingInf;
import com.busi.model.BindingLog;
import com.busi.model.PointsOrder;
import com.busi.util.HttpUtils;
import com.busi.util.PropertyUtil;
import com.busi.util.StringUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zcurd.controller.BaseController;
import com.zcurd.model.Contants;
import com.zcurd.model.ContantsRC;



  

/** ******************  类说明  *********************
 * class       :  BinDingInfController
 * @author     :  dangjingchao
 * @version    :  1.0  
 * description :  第三方账户绑定信息表
 * @see        :                        
 * ************************************************/   
public class BinDingInfController extends BaseController {
	
	 
	     
	/** ********************************************
	 * method name   : listPage 
	 * description   : 跳转到第三方绑定信息列表页面
	 * @return       : void
	 * @param        : 
	 * modified      : dangjingchao ,  2017年5月5日  下午9:28:45
	 * @see          : 
	 * ********************************************/      
	public void listPage(){
		render("infList.html");
	}
	
	/** ********************************************
	 * method name   : unBindingView 
	 * description   : 中国移动解绑页面
	 * @return       : void
	 * @param        : 
	 * modified      : zhangyajie ,  2017年6月28日  下午4:16:00
	 * @see          : 
	 * ********************************************/
	public void unBindingView() {
		LOG.info("#unBindingView() 中国移动解绑页面");
		BindingInf bindingInf = super.getBean(BindingInf.class, "");
		setAttr("binding", bindingInf);
		render("unbinding.html");
	}
	
	/** ********************************************
	 * method name   : listData 
	 * description   : 第三方绑定信息查询
	 * @return       : void
	 * @param        : 
	 * modified      : dangjingchao ,  2017年5月5日  下午9:29:12
	 * @see          : 
	 * ********************************************/      
	public void listData(){
		LOG.info("#listData() 查询第三方绑定信息");
		BindingInf bindingInf = super.getBean(BindingInf.class, "");
		String body = gson.toJson(bindingInf);
		LOG.info("#listData()请求参数:"+body);
		String domain = PropertyUtil.getStrValue("platform.butler.domain", "");
		String url = domain+"/admin/bindinginf/list";
		String[][] headers = {{"Content-Type", "application/json"}};
		String jsonString = HttpUtils.doPost(url, body, headers, 3, 5, "utf-8");
		LOG.debug("#listData() 第三方绑定信息列表:"+jsonString);
		
		if (StringUtil.isBlank(jsonString)){
			renderJson();
		} else {
			renderJson(jsonString);
		}
	}
	
	/** ********************************************
	 * method name   : unBinding 
	 * description   : 解绑
	 * @return       : void
	 * @param        : 
	 * modified      : dangjingchao ,  2017年5月6日  上午11:15:50
	 * @see          : 
	 * ********************************************/      
	public void unBinding(){
		String user_name = (String)getSessionUser().get("user_name");
		BindingInf bindingInf = super.getBean(BindingInf.class, "");
		String userid = bindingInf.getUserid();
		LOG.info("#unBinding() 解绑:"+userid);
		//设置用户绑定状态为解除绑定状态
		bindingInf.setState(Integer.parseInt("3"));
		//存入操作人
		bindingInf.setOperator(user_name);
		//发送请求
		String body = gson.toJson(bindingInf);
		LOG.info("#unBinding()请求参数:"+body);
		String domain = PropertyUtil.getStrValue("platform.butler.domain", "");
		String url = domain+"/admin/bindinginf/unbinding";
		String[][] headers = {{"Content-Type", "application/json"}};
		String ret = HttpUtils.doPost(url, body, headers, 3, 5, "utf-8");
		LOG.debug("#unBinding() 解除用户: "+userid+"绑定,"+ret);
		if("success".equals(ret)){
			LOG.info("#unBinding() 用户:"+userid+"解绑成功,操作人："+user_name);
			renderSuccess("解绑成功");
		}else{
			LOG.info("#unBinding() 用户:"+userid+"解绑失败,操作人："+user_name);
			renderFailed("解绑失败");
		}
	}
	
	/** ********************************************
	 * method name   : sendAuthCode 
	 * description   : 解绑发送验证码
	 * @return       : void
	 * @param        : 
	 * modified      : zhangyajie ,  2017年6月29日  上午11:15:50
	 * @see          : 
	 * ********************************************/      
	public void sendAuthCode(){
		String user_name = (String)getSessionUser().get("user_name");
		BindingInf bindingInf = super.getBean(BindingInf.class, "");
		String userid = bindingInf.getUserid();
		String outmobileid = bindingInf.getOutmobileid();
		LOG.info("#sendAuthCode() 管理平台下发验证码请求参数:" + userid + ", outmobileid:" + outmobileid);
		
		Map<String, String> result = new HashMap<String, String>();
	    //根据userid查对应的移动手机号
	    //查询绑定信息
		String baseurl = PropertyUtil.getStrValue("platform.butler.domain", "");
		String getBindingInfoUrl = baseurl + "/api/binding/getbindinginf/"+userid+"/CMCC";
		LOG.info("#sendAuthCode() 请求下发验证码请求地址:" + getBindingInfoUrl);
		String[][] headers = {{"Content-Type", "application/json"}};
		String retJson = HttpUtils.doGet(getBindingInfoUrl, headers, 3, 10, "utf-8");
		LOG.info("#sendAuthCode() 请求下发验证码相应参数:" + retJson);
		if(StringUtil.isBlank(retJson)){
			result.put("ret", "fail");
			result.put("msg", "该用户没有绑定信息");
			String content = gson.toJson(result);
			renderJson(content);
			return;
		}
		BindingInf binding = gson.fromJson(retJson, new TypeToken<BindingInf>(){}.getType());
		String mobileid = binding.getOutmobileid();
		if (StringUtil.isBlank(mobileid)) {
			result.put("ret", "fail");
			result.put("msg", "手机号为空");
			String content = gson.toJson(result);
			renderJson(content);
			return;
		}
		String url = baseurl + "/api/unbinding/sendauthcode/" + mobileid;
		String results = HttpUtils.doPost(url, null, headers, 3, 10, Contants.CHARSET_UTF_8);
		LOG.info("响应结果:" + results);
		if (StringUtil.isBlank(results)) {
			LOG.info("响应为空");
			result.put("ret", "fail");
			result.put("msg", "服务器忙，请稍后再试");
			String content = gson.toJson(result);
			renderJson(content);
			return;
		}
		
		//返回请求
		Map<String, String> fromJson = gson.fromJson(results, new TypeToken<Map<String, String>>() {}.getType());

		if (!ContantsRC.SUCCESS.equals(fromJson.get("ret"))) {
			LOG.info("发送失败：ret=" + fromJson.get("ret") +", msg=" + fromJson.get("msg"));
			result.put("ret", "fail");
			result.put("msg", (String) fromJson.get("msg"));
			String content = gson.toJson(result);
			renderJson(content);
			return;
		}
		result.put("ret", ContantsRC.SUCCESS);
		result.put("msg", fromJson.get("msg"));
		String content = gson.toJson(result);
		renderJson(content);
		return;
	}
	
	/** ********************************************
	 * method name   : CMCCUnBinding 
	 * description   : 中国移动解绑
	 * @return       : void
	 * @param        : 
	 * modified      : zhangyajie ,  2017年5月6日  上午11:15:50
	 * @see          : 
	 * ********************************************/      
	public void CMCCUnBinding(){
		String user_name = (String)getSessionUser().get("user_name");
		BindingInf bindingInf = super.getBean(BindingInf.class, "");
		String userid = bindingInf.getUserid();
		//先做验证码校验
		String optCode = bindingInf.getOptCode();
	    Map<String, String> map = new HashMap<String, String>();
	    Map<String, String> bindandact = new HashMap<String, String>();
	    bindandact.put("thirdAccount", userid);
	    bindandact.put("ipAddress", super.getClientIp(getRequest()));
	    bindandact.put("role", "01");//用户发起
	    String body =  gson.toJson(bindandact);
	    String baseurl = PropertyUtil.getStrValue("platform.butler.domain", "");
		LOG.info("#CMCCUnBinding() 第三方账户解绑,请求参数=" + body);
		String url = baseurl + "/api/unbinding/CMCC/"+ optCode;
		LOG.info("#CMCCUnBinding() 第三方账户解绑,请求url=" + url);
		String[][] headers = {{"Content-Type", "application/json"}};
		String retJson = HttpUtils.doPost(url, body, headers, 3, 10, Contants.CHARSET_UTF_8);
		LOG.info("#CMCCUnBinding() 第三方账户解绑,响应报文=" + retJson);
		if(StringUtil.isBlank(retJson)){
			map.put("ret", ContantsRC.SYS_ERR);
			map.put("msg", "系统异常");
			//返回值
			String content = gson.toJson(map);
			renderJson(content);
			return;
		}
		Map<String,String> ireqmap = gson.fromJson(retJson, new TypeToken<Map<String,String>>() {}.getType());
		map.put("ret", ireqmap.get("ret"));
		if(ContantsRC.SUCCESS.equals(ireqmap.get("ret")))
		{
			map.put("msg", "解绑成功");
		}else{
			map.put("msg", ireqmap.get("msg"));
		}
		//返回值
		String content = gson.toJson(map);
		renderJson(content);
		return;
	}
	
	/**
	 * ========================方法====说明==========================
	 * @Title: platformList void    
	 * @throws 
	 * @author GuoHonglin  Email:rootfive.guo@gmail.com
	 * @Description:  第三方账户管理：账户交易订单明细，平台商户查询
	 * 
	 */
	public void  platformList() {
		String platformMerchants = PropertyUtil.getStrValue("third.party.platform.merchants", "");
//		List<PlatformMerchant> list = gson.fromJson(platformMerchants, new TypeToken<List<PlatformMerchant>>() {
//		}.getType());
//		renderJson(gson.toJson(list));
		
		renderJson(platformMerchants);
	}
	
	/**
	 * ========================方法====说明==========================
	 * @Title: platformList void    
	 * @throws 
	 * @author GuoHonglin  Email:rootfive.guo@gmail.com
	 * @Description:  第三方账户管理：平台商户ID配置查询
	 * 
	 */
	public void  platformListConfig() {
		String platformMerchants = PropertyUtil.getStrValue("third.party.platform.merchants.config", "");
		renderJson(platformMerchants);
	}
}
